<?php


namespace app\api\controller;


use app\model\ModelTask;
use app\model\ModelUserTask;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\response\Json;

class SelfApi
{
    /**
     * 获取当前用户可领取任务列表
     * ajax userId
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getTaskList(): Json
    {
        $userId = input('userId');
        $userTask = new ModelUserTask();
        $userTaskInfo = $userTask->field('task_id')->where([
            'user_id' => $userId
        ])->select();
        $ids = [];
        foreach ($userTaskInfo as $task) {
            $ids[] = $task['task_id'];
        }
        $taskModel = new ModelTask();
        if (!empty($ids)) {
            $collection = $taskModel->whereNotIn('id', $ids)->where([
                'task_status' => 0,//任务状态可用
                'is_delete' => 0// 任务未删除
            ])->select();
        } else {
            $collection = $taskModel->where([
                'task_status' => 0,
                'is_delete' => 0
            ])->select();
        }

        $data = [
            'code' => 0,
            'count' => count($collection),
            'msg' => '',
            'data' => $collection
        ];
        return json($data);
    }

    /**
     * 获取当前用户已领取任务列表
     * ajax userId
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getMyTask(): Json
    {
        $userId = input('userId');
        $userTask = new ModelUserTask();
        $taskModel = new ModelTask();
        $myTasks = $userTask->where([
            'user_id' => $userId,
        ])->select();
        $myTaskList = [];
        foreach ($myTasks as $task) {
            $taskInfo = $taskModel->where('id', '=', $task['task_id'])->find();
            $taskInfo['task_status'] = $task['task_status'];//用户任务状态
            $taskInfo['user_id']  = $task['user_id'];//用户id
            $taskInfo['task_start_time'] = $task['task_start_time'];
            $taskInfo['task_complete_time'] = $task['task_complete_time'];
            $myTaskList[] = $taskInfo;
        }

        $data = [
            'code' => 0,
            'count' => count($myTaskList),
            'msg' => '',
            'data' => $myTaskList
        ];
        return json($data);
    }

    /**
     * 获取当前用户指定任务 信息
     * ajax userId taskId
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getUserTaskByUserIdAndTaskId(): array{
        $userId = input('userId');
        $taskId = input('taskId');
        $userTaskModel = new ModelUserTask();
        return $userTaskModel->where([
            'userId' => $userId,
            'taskId' => $taskId
        ])->find()->toArray();
    }


    public function getPlatformReceivable() {
        /*
         * type:账户类型
         * 101 : 银行卡
         * 201 : 支付宝
         * 301 : 微信
         */
        $type = input('type');
        $where = [];
        $field = [];
        if($type == 101) {
            $where = [
                'type' => 101,
                'status' => 1
            ];
        }
    }

}